<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/6
 * Time: 14:43
 */

namespace App\Services;


use Illuminate\Support\Facades\Redis;
use Mrgoon\AliSms\AliSms;

class SendSms
{


    //获取随机数
    public static function code()
    {
        $randNumber=mt_rand(100000,999999);
        str_shuffle($randNumber);
        return $randNumber;
    }

    //判断手机号是否被注册
    public static function checkPhone()
    {

    }

    //发送短信验证码
    public static function sendMessage($phone)
    {
        $aliSms = new AliSms();
        $code = self::code();
        $response = $aliSms->sendSms($phone, 'SMS_142948647', ['code'=> $code]);
        if($response->Code != 'OK'){
            throw new \Exception('短信发送失败');
        }
        //短信获取成功缓存手机号设置5分钟
        self::setOutTime($phone,300,$code);
        return true;
    }

    //判断验证码是否正确
    public static function checkCode($phone,$code)
    {
        $trueCode = Redis::get($phone);
        if($trueCode != $code){
            throw new \Exception('验证码错误或已经过期');
        }
        return true;
    }

    //设置过期时间
    public static function setOutTime($phone,$time,$code)
    {
        Redis::setex($phone, $time , $code);
        return true;
    }

    //重新获取验证码删除原缓存
    public function anewCode()
    {
        
    }

}